package com.catal.framework.query;

import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;
import net.sf.jsqlparser.JSQLParserException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import com.catal.framework.data.Condition;
import com.catal.framework.data.QueryDataList;
import com.catal.framework.util.OracleSqlPareserUtil;

public class QueryServiceImpl extends JdbcDaoSupport implements QueryService{
	
	protected static Log log = LogFactory.getLog(QueryServiceImpl.class);
	
	//完整的sql语句
	private String sql;
	
//	//将sql语句分段 - 查询的列
//	private String coulumns;
//	
//	//将sql语句分段 - 最外层的表
//	private String table;
//	
//	//将sql语句分段 - 最外层的where条件
//	private String where;
//	
//	//将sql语句分段 - 最终的排序
//	private String order;
	
	//统计的ID 
	private String countId = "*";
	
	public QueryDataList query3(Map p) {
		QueryDataList qdl = new QueryDataList();
		Condition c = (Condition) p.get("condition");
		String querySql = "";
		String countSql = "";
		if(sql != null && !"".equals(sql)){
			querySql = sql;
			countSql = "select count(*) from (" + querySql + ") t";
		}else{
			
		}
		
		List<Map<String,Object>> list = this.getJdbcTemplate().queryForList(querySql);
		int count = this.getJdbcTemplate().queryForInt(countSql);
		qdl.setRows(list);
		qdl.setTotal(count);
//		JSONObject jsonObject = JSONObject.fromObject(qdl);
		return qdl;
	}
	
	public QueryDataList query(int page, int pagesize, String[] orderFields,
			Condition condition, boolean calcRowCount) {
		log.info("查询条件:" + condition);
		QueryDataList qdl = new QueryDataList();
		String querySql = "";
		String countSql = "";
		if(sql != null && !"".equals(sql)){
			try {
				querySql = OracleSqlPareserUtil.generateQuerySql(sql, (page-1)*pagesize+1, pagesize, condition,orderFields);
				countSql = OracleSqlPareserUtil.generateCountSql(sql, condition, countId);
				log.info(querySql);
				log.info(countSql);
			} catch (JSQLParserException e) {
				//写日志记录
				log.error(e.getMessage());
				throw new RuntimeException("sql语句有误查询失败");
			}
		}else{
			return null;
		}
		List<Map<String,Object>> list = this.getJdbcTemplate().queryForList(querySql);
		int count = this.getJdbcTemplate().queryForInt(countSql);
		qdl.setRows(list);
		qdl.setTotal(count);
		return qdl;
	}
	
	public String query2Json(int offset, int pagesize, String[] orderFields,
			Condition condition, boolean calcRowCount) {
		QueryDataList qdl = new QueryDataList();
		String querySql = "";
		String countSql = "";
		try {
			querySql = OracleSqlPareserUtil.generateQuerySql(sql, offset, pagesize, condition,orderFields);
			countSql = OracleSqlPareserUtil.generateCountSql(sql, condition, countId);
		} catch (JSQLParserException e) {
			//写日志记录
			log.error(e.getMessage());
			throw new RuntimeException("sql语句有误查询失败");
		}
//		countSql = "SELECT COUNT(" + countId + ") FROM " + 
//		List<Map<String,Object>> list = this.getJdbcTemplate().queryForList(querySql);
//		int count = this.getJdbcTemplate().queryForInt(countSql);
//		qdl.setDatas(list);
//		qdl.setTotalRecodes(count);
		JSONObject jsonObject = JSONObject.fromObject(qdl);
		return jsonObject.toString();
	}
	
	private String generateSql(){
		return null;
	}
	
	private String generateCountSql(String sql){
		String countSql = "select " + countId;
		return null;
	}


	public String getCountId() {
		return countId;
	}

	public void setCountId(String countId) {
		this.countId = countId;
	}

	public String getSql() {
		return sql;
	}

	public void setSql(String sql) {
		this.sql = sql;
	}

}
